package com.cnap.service.impl;

import cn.hutool.http.HttpResponse;
import com.cnap.CnapApplicationTests;
import com.cnap.model.cloud.CreateCloudParam;
import com.cnap.model.cloud.DeleteLocalImageParam;
import com.cnap.model.cloud.DeleteParam;
import com.cnap.model.exception.JsonException;
import com.cnap.service.CloudMgmtService;
import com.cnap.utils.JsonUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class CloudMgmtServiceImplTest extends CnapApplicationTests {

    @Autowired
    private CloudMgmtService cloudMgmtService;

    @Test
    void createCloudResourceWithError() {
        CreateCloudParam param = new CreateCloudParam();
        HttpResponse response = Mockito.mock(HttpResponse.class);

        Mockito.when(mockRequest.body(JsonUtil.objectToJsonStr(param))).thenReturn(mockRequest);
        Mockito.when(mockRequest.execute()).thenReturn(response);

        Mockito.when(response.isOk()).thenReturn(false);


        Assertions.assertAll(
                () -> {
                    Assertions.assertThrows(JsonException.class, () -> {
                        cloudMgmtService.createCloudResource(param);
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteLocalImageByServiceInfo(new DeleteLocalImageParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteVmAndOtherInfo(new DeleteParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.insertNewScalingPolicy(Collections.emptyList());
                    });
                }
        );
    }

    @Test
    void createCloudResourceWithError1() {
        CreateCloudParam param = new CreateCloudParam();
        HttpResponse response = Mockito.mock(HttpResponse.class);

        Mockito.when(mockRequest.body(JsonUtil.objectToJsonStr(param))).thenReturn(mockRequest);
        Mockito.when(mockRequest.execute()).thenReturn(response);

        String s = "{\n" +
                "  \"code\": 210,\n" +
                "  \"message\": \"string\",\n" +
                "  \"success\": true\n" +
                "}";

        Mockito.when(response.isOk()).thenReturn(true);
        Mockito.when(response.body()).thenReturn(s);

        Assertions.assertAll(
                () -> {
                    Assertions.assertThrows(JsonException.class, () -> {
                        cloudMgmtService.createCloudResource(param);
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteLocalImageByServiceInfo(new DeleteLocalImageParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteVmAndOtherInfo(new DeleteParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.insertNewScalingPolicy(Collections.emptyList());
                    });
                }
        );
    }

    @Test
    void createCloudResource() {
        CreateCloudParam param = new CreateCloudParam();
        HttpResponse response = Mockito.mock(HttpResponse.class);

        Mockito.when(mockRequest.body(JsonUtil.objectToJsonStr(param))).thenReturn(mockRequest);
        Mockito.when(mockRequest.execute()).thenReturn(response);

        String s = "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"string\",\n" +
                "  \"success\": true\n" +
                "}";

        Mockito.when(response.isOk()).thenReturn(true);
        Mockito.when(response.body()).thenReturn(s);

        Assertions.assertAll(
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.createCloudResource(param);
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteLocalImageByServiceInfo(new DeleteLocalImageParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteVmAndOtherInfo(new DeleteParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.insertNewScalingPolicy(Collections.emptyList());
                    });
                }
        );
    }

    @Test
    void createCloudResourceWithError2() {
        CreateCloudParam param = new CreateCloudParam();
        HttpResponse response = Mockito.mock(HttpResponse.class);

        Mockito.when(mockRequest.body(JsonUtil.objectToJsonStr(param))).thenReturn(mockRequest);
        Mockito.when(mockRequest.execute()).thenReturn(response);

        String s = "{\n" +
                "  \"code\": 210,\n" +
                "  \"message\": \"string\",\n" +
                "  \"error\": true\n" +
                "}";

        Mockito.when(response.isOk()).thenReturn(true);
        Mockito.when(response.body()).thenReturn(s);

        Assertions.assertAll(
                () -> {
                    Assertions.assertThrows(JsonException.class, () -> {
                        cloudMgmtService.createCloudResource(param);
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteLocalImageByServiceInfo(new DeleteLocalImageParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.deleteVmAndOtherInfo(new DeleteParam());
                    });
                },
                () -> {
                    Assertions.assertDoesNotThrow(() -> {
                        cloudMgmtService.insertNewScalingPolicy(Collections.emptyList());
                    });
                }
        );
    }
}